package com.tpshion.cloud.common.support;

import java.io.Serializable;

/**
 * 统一结果返回
 * @param <T>
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 4066473065776722843L;

    private String code;

    private String msg;

    private T data;

    private boolean status;

    public Result(){

    }

    public Result(String code, String msg, T data, boolean status){
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.status = status;
    }

    public static <T> Result ok(){
        return new Result<T>(ErrorCode.OK.getCode(), ErrorCode.OK.getMsg(),null,true);
    }

    public static <T> Result ok(T data){
        return new Result<T>(ErrorCode.OK.getCode(), ErrorCode.OK.getMsg(),data,true);
    }

    public static <T> Result fail(String code, String msg){
        return new Result<T>(code,msg,null,false);
    }

    public static <T> Result fail(String msg){
        return new Result<T>(ErrorCode.FAIL.getCode(),msg,null,false);
    }

    public static <T> Result build(ErrorCode errorCode){
        return new Result<T>(errorCode.getCode(),errorCode.getMsg(),null,false);
    }

    public static <T> Result build(String code, String msg, T data, boolean status){
        return new Result<T>(code,msg,data,status);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                ", status=" + status +
                '}';
    }
}
